package com.zheng.wechat.mp.admin.controller.manage;

import com.zheng.common.base.BaseController;
import com.zheng.common.constant.Constant;
import com.zheng.common.util.JsonUtil;
import com.zheng.pay.dao.model.PayMchInfo;
import com.zheng.pay.dao.model.PayMchInfoExample;
import com.zheng.pay.rpc.api.PayMchInfoService;
import com.zheng.upms.dao.model.UpmsAgent;
import com.zheng.upms.dao.model.UpmsAgentExample;
import com.zheng.upms.rpc.api.UpmsApiService;
import com.zheng.wechat.mp.common.constant.WechatMpResult;
import com.zheng.wechat.mp.common.constant.WechatMpResultConstant;
import com.zheng.wechat.mp.dao.model.WechatMpWebJwid;
import com.zheng.wechat.mp.dao.model.WechatMpWebJwidExample;
import com.zheng.wechat.mp.rpc.api.WechatMpWebJwidService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/11/8.
 */
@Controller
@RequestMapping("/manage/thirdapp")
@Api(value = "公号管理控制器", description = "公号管理控制器")
public class ThirdAppController extends BaseController {
    private static final Logger LOGGER = LoggerFactory.getLogger(ThirdAppController.class);

    @Autowired
    private WechatMpWebJwidService wechatMpWebJwidService;

    @Autowired
    private UpmsApiService upmsApiService;
    @Autowired
    private PayMchInfoService payMchInfoService;
    /**
     * app管理首页
     * @return
     */
    @ApiOperation(value = "公号管理首页")
    @RequiresPermissions("wechatmp:thirdapp:read")
    @RequestMapping(value = "/index", method = {RequestMethod.GET, RequestMethod.POST})
    public String index() {
        return "/manage/thirdapp/index.jsp";
    }


    @ApiOperation(value = "公号列表")
    @RequiresPermissions("wechatmp:thirdapp:read")
    @RequestMapping(value = "/list", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Object list(
            @RequestParam(required = false, defaultValue = "", value = "search") String search,
            @RequestParam(required = false, value = "sort") String sort,
            @RequestParam(required = false, value = "order") String order) {
        WechatMpWebJwidExample wechatMpWebJwidExample = new WechatMpWebJwidExample();
        if (!StringUtils.isBlank(sort) && !StringUtils.isBlank(order)) {
            wechatMpWebJwidExample.setOrderByClause(sort + " " + order);
        }else{
            wechatMpWebJwidExample.setOrderByClause("id asc");
        }
        if (StringUtils.isNotBlank(search)) {
            wechatMpWebJwidExample.createCriteria().andNameLike("%" + search + "%");
        }
        List<WechatMpWebJwid> rows = wechatMpWebJwidService.selectByExample(wechatMpWebJwidExample);
        Map<String, Object> result = new HashMap<>();
        result.put("rows", rows);
        return result;
    }

    @ApiOperation(value = "新增公号")
    @RequiresPermissions("wechatmp:thirdapp:create")
    @RequestMapping(value = "/create", method = {RequestMethod.GET})
    public String create(ModelMap modelMap) {

        UpmsAgentExample example = new UpmsAgentExample();
        example.createCriteria().andFlagEqualTo(Constant.CONST_FLAG_NORMAL);
        List<UpmsAgent> upmsAgents = upmsApiService.selectUpmsAgentByExample(example);
        modelMap.put("upmsAgents", JsonUtil.object2Json(upmsAgents));

        PayMchInfoExample mchInfoExample = new PayMchInfoExample();
        mchInfoExample.createCriteria().andStateEqualTo((byte)Constant.CONST_FLAG_NORMAL);
        List<PayMchInfo> payMchInfos = payMchInfoService.selectByExample(mchInfoExample);
        modelMap.put("payMchInfos", payMchInfos);

        return "/manage/thirdapp/create.jsp";
    }

    @ApiOperation(value = "新增公号")
    @RequiresPermissions("wechatmp:thirdapp:create")
    @ResponseBody
    @RequestMapping(value = "/create", method = {RequestMethod.POST})
    public Object create(WechatMpWebJwid wechatMpWebJwid ) {
        Date time = new Date();
        wechatMpWebJwid.setCreateTime(time);
        wechatMpWebJwid.setCreateBy(getLoginUserName());
        int count = wechatMpWebJwidService.insertSelective(wechatMpWebJwid);
        return new WechatMpResult(WechatMpResultConstant.SUCCESS, count);
    }

    @ApiOperation(value = "删除公号")
    @RequiresPermissions("wechatmp:thirdapp:delete")
    @RequestMapping(value = "/delete/{ids}",method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Object delete(@PathVariable("ids") String ids) {
        int count = wechatMpWebJwidService.deleteByPrimaryKeys(ids);
        return new WechatMpResult(WechatMpResultConstant.SUCCESS, count);
    }

    @ApiOperation(value = "修改公号")
    @RequiresPermissions("wechatmp:thirdapp:update")
    @RequestMapping(value = "/update/{id}", method = {RequestMethod.GET})
    public String update(@PathVariable("id") int id, ModelMap modelMap) {
        WechatMpWebJwid wechatMpWebJwid  = wechatMpWebJwidService.selectByPrimaryKey(id);

        if(wechatMpWebJwid.getAgentId()!=null){
            UpmsAgent upmsAgent = upmsApiService.selectUpmsAgentByPrimaryKey(wechatMpWebJwid.getAgentId());
            modelMap.put("agent", upmsAgent);
        }
        UpmsAgentExample example = new UpmsAgentExample();
        example.createCriteria().andFlagEqualTo(Constant.CONST_FLAG_NORMAL);
        List<UpmsAgent> upmsAgents = upmsApiService.selectUpmsAgentByExample(example);
        modelMap.put("upmsAgents", JsonUtil.object2Json(upmsAgents));

        PayMchInfoExample mchInfoExample = new PayMchInfoExample();
        mchInfoExample.createCriteria().andStateEqualTo((byte)Constant.CONST_FLAG_NORMAL);
        List<PayMchInfo> payMchInfos = payMchInfoService.selectByExample(mchInfoExample);
        modelMap.put("payMchInfos", payMchInfos);

        modelMap.put("wechatMpWebJwid", wechatMpWebJwid);
        return "/manage/thirdapp/update.jsp";
    }

    @ApiOperation(value = "修改公号")
    @RequiresPermissions("wechatmp:thirdapp:update")
    @RequestMapping(value = "/update/{id}", method = {RequestMethod.POST})
    @ResponseBody
    public Object update(@PathVariable("id") int id, WechatMpWebJwid wechatMpWebJwid ) {
        wechatMpWebJwid.setId(id);
        int count = wechatMpWebJwidService.updateByPrimaryKeySelective(wechatMpWebJwid);
        return new WechatMpResult(WechatMpResultConstant.SUCCESS, count);
    }


}
